{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 08\n",
    "# 自定义函数中的帮助文档\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 计算向量内积\n",
    "def inner_prod(a, b):\n",
    "    '''\n",
    "    自定义函数计算两个向量内积\n",
    "    输入：\n",
    "    a：向量，类型为数据列表\n",
    "    b：向量，类型为数据列表\n",
    "    输出：\n",
    "    c：标量\n",
    "    参考：\n",
    "    https://mathworld.wolfram.com/InnerProduct.html\n",
    "    '''\n",
    "\n",
    "    # 检查两个向量元素数量是否相同\n",
    "    if len(a) != len(b):\n",
    "        raise ValueError(\"Error: check a/b lengths\")\n",
    "\n",
    "    # 初始化内积为0\n",
    "    dot_product = 0\n",
    "    # 使用for循环计算内积\n",
    "    for i in range(len(a)):\n",
    "        dot_product += a[i] * b[i]\n",
    "\n",
    "    return dot_product"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:07:30.917863Z",
     "end_time": "2024-07-03T16:07:30.929341Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function inner_prod in module __main__:\n",
      "\n",
      "inner_prod(a, b)\n",
      "    自定义函数计算两个向量内积\n",
      "    输入：\n",
      "    a：向量，类型为数据列表\n",
      "    b：向量，类型为数据列表\n",
      "    输出：\n",
      "    c：标量\n",
      "    参考：\n",
      "    https://mathworld.wolfram.com/InnerProduct.html\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 查询自定义函数文档，两种办法\n",
    "help(inner_prod)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:07:30.931340Z",
     "end_time": "2024-07-03T16:07:30.937139Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    自定义函数计算两个向量内积\n",
      "    输入：\n",
      "    a：向量，类型为数据列表\n",
      "    b：向量，类型为数据列表\n",
      "    输出：\n",
      "    c：标量\n",
      "    参考：\n",
      "    https://mathworld.wolfram.com/InnerProduct.html\n",
      "    \n"
     ]
    }
   ],
   "source": [
    "print(inner_prod.__doc__)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:07:30.938146Z",
     "end_time": "2024-07-03T16:07:30.943054Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "# 定义向量a和b\n",
    "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "b = a[::-1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:07:30.944052Z",
     "end_time": "2024-07-03T16:07:30.947833Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "# 调用函数\n",
    "c = inner_prod(a, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:07:30.950180Z",
     "end_time": "2024-07-03T16:07:30.996117Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "向量内积为： 165\n"
     ]
    }
   ],
   "source": [
    "# 打印内积\n",
    "print(\"向量内积为：\", c)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:07:30.955708Z",
     "end_time": "2024-07-03T16:07:31.008761Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
